﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvdDonk.Bomberman.Menu;
using MvdDonk.Bomberman.Screens.Base;
using Microsoft.Xna.Framework;

namespace MvdDonk.Bomberman.Screens
{
    class JoinServerScreen : MenuScreen
    {
        private double lookForServersDelay = 4000;
        private double lookForServersTimer = 0;

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public JoinServerScreen()
            : base("Available hosts", MenuOrientation.Vertical)
        {
            // Create our menu entries.
            MenuEntry loadingGameMenuEntry = new MenuEntry("Looking for servers...");
            loadingGameMenuEntry.Selectable = false;

            // Hook up menu event handlers.
            //optionsMenuEntry.Selected += OptionsMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(loadingGameMenuEntry);

            Bomberman.Instance.NetworkManager.FindServers();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            foreach (var server in Bomberman.Instance.NetworkManager.ServersFound)
            {
                if (!MenuEntries.Any(m => m.Text == server.Key))
                {
                    var entry = new MenuEntry(server.Key);
                    entry.Selected += new EventHandler<EventArgs>(entry_Selected);
                    MenuEntries.Add(entry);
                }
            }
            if (Bomberman.Instance.NetworkManager.ServersFound.Count > 0)
                MenuEntries.RemoveAll(m => !Bomberman.Instance.NetworkManager.ServersFound.ContainsKey(m.Text));


            lookForServersTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (lookForServersTimer >= lookForServersDelay)
            {
                lookForServersTimer = 0;
                Bomberman.Instance.NetworkManager.FindServers();
            }
        }

        protected override void OnCancel()
        {
            Bomberman.Instance.NetworkManager.KillClient();
            base.OnCancel();
        }

        public void PopupCancelled()
        {
            OnCancel();
        }

        void entry_Selected(object sender, EventArgs e)
        {
            var endPoint = Bomberman.Instance.NetworkManager.ServersFound.FirstOrDefault(s => s.Key == ((MenuEntry)sender).Text).Value;
            if (endPoint != null)
            {
                Bomberman.Instance.NetworkManager.JoinServer(endPoint);

                //if (Bomberman.Instance.NetworkManager.Client.IsConnected)
                ScreenManager.RemoveScreen(this);
                ScreenManager.AddScreen(new WaitingOnHostServerScreen(this));
            }
            else
                throw new NotImplementedException("Couldn't connect to server, should be error box");
        }
    }
}
